const express = require('express')
const router = express.Router()
const passport = require('passport')
const Cuisine = require('../../models/Cuisine')
const pagination = require('mongoose-sex-page')

router.get('/Cuisine/list/all', passport.authenticate('jwt', { session: false }), (req, res) => {
  // page 当前页
  Cuisine.find()
    .then((Cuisine) => {
      if (!Cuisine) {
        return res.status(400).json({ status: 400, message: '获取套餐列表失败', data: [] })
      }
      const total = Cuisine.length
      return res.json({ status: 200, message: '请求成功', success: true, data: Cuisine, total })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '获取套餐失败' })
    })
})

router.get('/Cuisine/list', passport.authenticate('jwt', { session: false }), (req, res) => {
  let CuisineFields = {}
  if (req.query.models) CuisineFields.models = req.query.models
  if (req.query.FoodAddress) CuisineFields.FoodAddress = req.query.FoodAddress
  if (req.query.CuisinePrice) CuisineFields.CuisinePrice = req.query.CuisinePrice
  if (req.query.date) CuisineFields.date = req.query.date
  if (req.query.CuisineFlag == 'true' || req.query.CuisineFlag == 'false') {
    // console.log(444)
    CuisineFields.CuisineFlag = req.query.CuisineFlag
  }
  // page 当前页
  pagination(Cuisine)
    .find(CuisineFields)
    .page(req.query.pageNum)
    .size(req.query.pageSize)
    .exec()
    .then((Cuisine) => {
      if (!Cuisine) {
        return res.status(400).json({ status: 400, message: '获取用户列表失败', data: [] })
      }
      const total = Cuisine.length
      return res.json({ status: 200, message: '请求成功', success: true, data:Cuisine, total })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '获取列表失败' })
    })
})

router.post('/Cuisine/add', passport.authenticate('jwt', { session: false }), (req, res) => {
  let CuisineField = {}
  if (req.body.models) CuisineField.models = req.body.models
  if (req.body.FoodAddress) CuisineField.FoodAddress = req.body.FoodAddress
  if (req.body.CuisinePrice) CuisineField.CuisinePrice = req.body.CuisinePrice
  if (req.body.date) CuisineField.date = req.body.date
  if (req.body.CuisineImg) CuisineField.CuisineImg = req.body.CuisineImg
  new Cuisine(CuisineField)
    .save()
    .then(() => {
      return res.json({ status: 200, success: true, message: '添加成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '添加失败!' })
    })
})

router.post('/Cuisine/edit/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
  let CuisineField = {}
  if (req.body.models) CuisineField.models = req.body.models
  if (req.body.FoodAddress) CuisineField.FoodAddress = req.body.FoodAddress
  if (req.body.CuisinePrice) CuisineField.CuisinePrice = req.body.CuisinePrice
  if (req.body.date) CuisineField.date = req.body.date
  if (req.body.CuisineImg) CuisineField.CuisineImg = req.body.CuisineImg
  console.log(CuisineField,'CuisineField')
  console.log(req.params.id,'id')
  Cuisine.findOneAndUpdate({ _id: req.params.id },{ $set: CuisineField },{ new: true })
    .then(() => {
      return res.json({ status: 200, success: true, message: '修改成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '修改失败!' })
    })
})

router.delete('/Cuisine/delete/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
  Cuisine.findOneAndDelete({ _id: req.params.id })
    .then((Cuisine) => {
      
      return res.json({ status: 200, success: true, message: '删除成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '删除失败!' })
    })
})

router.get('/Cuisine/detail/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
  CuisineField.findOne({ _id: req.params.id })
    .then((Cuisine) => {
      return res.json({ status: 200, message: '请求成功', success: true, data: Cuisine })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, message: ' 获取信息失败', error })
    })
})

module.exports = router
